/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
var the;

// type = 1 限制单选 2 不限制
function selectFlowUser(th, type) {
    the = th;
    $.ajax({
        type: "get",
        url: sysInfo.basePath + "/sc/user/flow.html",
        dataType: 'html',
        success: function (res) {
            window.parent.DGG.Layer.open(res, {title: '用户选择', area: ['60%', '100%']}, function (index) {
                var ids = [];
                $("#myUserTable1").find('[name="userIds"]').each(function (i, V) {
                    if ($(this).is(':checked')) {// 选中的
                        console.log($(this).val());
                        ids.push($(this).val());
                    }
                });
                var idsStr = "";
                if (ids.length > 0) {
                    if (type == 1 && ids.length != 1) {
                        DGG.Layer.message("只能选择单个用户！", {icon: '1'});
                    } else {
                        layer.close(index);
                        for (var i = 0; i < ids.length; i++) {
                            idsStr += (ids[i] + ",");
                        }
                        idsStr = idsStr.substring(0, idsStr.length - 1);
                        selectFlowUserOnly(idsStr);
                    }
                } else {
                    DGG.Layer.message("选择用户！", {icon: '1'});
                    return false;
                }
            })
        }
    })
}

//新增选择用户可删除功能 dmk
function selectFlowUserOnly(idsStr) {
    $.ajax({
        type: "post",
        url: sysInfo.basePath + "/sc/user/selectUserIdByName.do",
        dataType: 'json',
        data: {idsStr: idsStr},
        success: function (res) {
            var userArr = $('#businessUserId').data('assistArray');
            userArr = userArr == null ? new Array() : userArr;
            for (var i = 0; i < res.length; i++) {
                var item = res[i];
                var have = false;
                for (var j in  userArr) {
                    if (item.id == userArr[j].id) {
                        have = true;
                        break;
                    }
                }
                if (!have) {
                    item.realName += '(' + item.orgName + ')'; //拼上部门
                    userArr.push(item);
                }
            }
            crateUserIcon(userArr);

        }
    });
}
//新增选择用户可删除功能 dmk
function removeName(event, obj) {
    var $d = $(obj);
    id = $d.parent().attr('data-id'), valArr = $('#businessUserId').data('assistArray');
    var index = -1;
    for (var i in valArr) {
        if (id == valArr[i].id) {
            index = i;
        }
    }
    if (index >= 0) {
        valArr.splice(index, 1);
        $d.parent().remove();
    }
    crateUserIcon(valArr);
    event.stopPropagation();
}

function emptyValue() {
    $("#businessUserId").val("");
    $("#businessUserName").val("");
}

$.fn.OpenSelectLayer = function (entity, option) {
    $(this).attr("readonly", true);
    $(this).on("click", function () {
        var entities = {
            User: "用户",
            Product: "商品"
        };
        var setting = {
            limit: 30,//最多选择数据条数
            fetchData: undefined,//接收返回数据的方法
            width: "60%",
            height: "80%",
            floor: window,
            invoker: this
        };

        if (option != undefined) {
            $.extend(setting, option);
        }
        if (entity == undefined) return;

        entity = entity.substring(0, 1).toUpperCase() + entity.substring(1);
        setting.floor.DGG.Layer.open("/sc/general/commonLayer/select" + entity + ".html", {
            title: "选择" + entities[entity],
            area: [setting.width, setting.height],
            btn: "",
            success: function (layer, index) {
                var fd = $(layer).find("iframe")[0].contentWindow.document;
                fd.selectLimit = setting.limit;
                fd.fetchData = setting.fetchData;
                fd.selectInvoker = setting.invoker;
            }
        });

    });

};





